<template>
  <div style="width: 100%">
    <div id="deawer">
      <v-tableTitle :btnConfig="btnConfig"
                    @cancel="cancel"
                    @save="save"></v-tableTitle>
      <div class="detailBox">
        <v-detail ref="detail"></v-detail>
      </div>
      <div class="detailListBox">
        <v-detailList ref="detailList"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import config from "../Config";
import tableTitle from "commons/TableTitle";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"

export default {
  name: "",
  mixins: [baseMixin, skipMixin, config],
  data () {
    return {
      newFlag: false
    };
  },
  computed: {
    btnConfig () {
      return [
        { label: this.$t('common.save'), certigier: this.saveStatus, invalid: "save" },
        { label: this.$t('common.return'), invalid: "cancel" },
      ]
    }
  },
  created () {
  },
  methods: {
    //保存按钮
    async save () {
      let detail = this.$refs.detail.detailData;
      if (detail.carrierkey == '') {
        this.$message.error(this.$tj('bill.carrierCode','app.emptyMes'));
        return;
      }
      let detailList = this.$refs.detailList.detailData;
      let data = {};
      data = {
        ...detail,
        gatherdetaillist: [{ ...detailList }]
      };
      if (this.newFlag) {
        try {
          let res = await this.$api.post('/saveGather', data);
          
            this.$message.success(res.returnMgr);
            this.$store.commit("setDetailsNew", false);
            this.cancel();
          
        } catch (e) {
          this.$message.error(e)
        }
      } else {
        if (this.$refs.detailList.detailNav == 2) {
          try {
            let res = await this.$api.post('/saveGatherDetail', detailList);
            
              this.$message.success(res.returnMgr);
              this.$store.commit("setDetailsNew", false);
              this.cancel();
            
          } catch (e) {
            this.$message.error(e)
          }
        } else {
          this.cancel();
        }
      }

    },
    //弹框取消
    cancel () {
      this.$router.push({ name: "gather" });
    },
  },
  mounted () {
    if (Object.keys(this.$store.state.skip).length !== 0) {
      this.newFlag = data.newFlag;
    } else {
      this.newFlag = this.$store.state.newFlag;
    }
  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
  }
}
</script>

<style scoped>
</style>
